<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class IndexSite extends BaseModel
{
    //定义表名 工地表
    protected $name = 'longbing_card_decorate_site';
    protected $append = [
        'area_text',
        'style_text',
        'housewear_text',
        'progress_text',
        'dyn_text',
        'is_collect'
    ];

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '=', $status);
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取进度
     */
    public function getProgressTextAttr($value,$data){
        $model = new AdminProgress();
        return $model->progressTitle(['id'=>$data['progress']]);
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getAreaTextAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->areaTitle(['id'=>$data['area']]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取风格名字
     */
    public function getStyleTextAttr($value,$data){
        $style_model = new AdminStyle();
        return $style_model->styleTitle(['id'=>$data['style']]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-15 11:57
     * @param $value
     * @param $data
     * @return mixed
     * descption:居室名字
     */
    public function getHousewearTextAttr($value,$data){
        $wear_model = new AdminHousewear();
        return $wear_model->housewearTitle(['id'=>$data['housewear']]);
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],',');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 17:35
     * @param $value
     * @return void
     * descption:带单位
     */
    public function getMAttr($value){
        return $value.'㎡';
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 17:35
     * @param $value
     * @return void
     * descption:带单位
     */
    public function getTotalPriceAttr($value){
        return $value.'万';
    }
    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换vr封面图
     */
    public function getVrCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 14:04
     * @param $value
     * @param $data
     * @return void
     * descption:获取区域
     */
    public function getAreaAttr($value,$data){
        $area_model = new AdminArea();
        return $area_model->reset($data['area']);
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 18:45
     * @param $value
     * @param $data
     * @return void
     * descption:获取动态
     */
    public function getDynTextAttr($value,$data){
        $dyn_model = new AdminDynamic();
        $dyn = $dyn_model->dynSelect(['a.site_id'=>$data['id'],'a.status'=>1]);
        return $dyn;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 工地列表
     */
    public function siteList($dis,$page = 10,$dis_price=array(),$dis_m=array(),$sort=0,$alh='id'){
        $sort = $this->sort($sort);
        $data = $this->where($dis)->
        where(function ($query) use ($dis_price){
            $query->whereOr($dis_price);
        })->where(function ($query) use ($dis_m){
            $query->whereOr($dis_m);
        })
          ->field(['*',$alh])
          ->order($sort)
          ->paginate($page)
            ->each(function ($val){
                $val['distance'] = round($val['distance'],2);
                return $val;
            })
          ->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 14:05
     * @param $dis
     * @return void
     * descption:员工工地列表
     */
    public function staffSiteList($dis){
        $data = $this->alias('a')
                ->join('longbing_card_decorate_staff_site b','a.id = b.site_id')
                ->where($dis)
                ->field('a.*')
                ->group('a.id')
                ->order('a.sort')
                ->paginate(10)
                ->toArray();
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-10-18 15:57
     * @param $sort
     * @return string
     * descption:排序
     */
    public function sort($sort){
        $data = '';
        switch ($sort){
            case 0:
                $data = 'sort desc';
                break;
            case 1:
                //访问量 iv
                $data = 'sort desc , iv desc ';
                break;
            case 2:
                //收藏量 collect
                $data = 'sort desc ,  collect desc';
                break;
            case 3:
                //距离 distance
                $data = 'distance asc,sort desc';
                break;
        }
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加工地
     */
    public function siteAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑工地
     */
    public function siteUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * 工地5条
     */
    public function siteLimit($dis){
        $data = $this->where($dis)->status(1)->order('sort desc')->limit(5)->select()->toArray();
        return $data;
    }
    /**
     * @param $dis
     * 工地详情
     */
    public function siteInfo($dis){
        $data = $this->where($dis)->status(1)->order('sort desc')->find();
        return !empty($data)?$data->toArray():$data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 16:36
     * @return void
     * descption:转换数据
     */
    public function changeData($data){
        $data['area'] = !empty($data['area'])?array_pop($data['area']):'';
        $data['imgs'] = !empty($data['area'])?implode(',',$data['area']):'';
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-10-18 16:00
     * @param $lng1
     * @param $lat1
     * @param $lng2
     * @param $lat2
     * @return float|int
     * descption:获取距离
     */
    public function getdistance($lng1, $lat1, $lng2, $lat2) {
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }



}